//
//  YGActionSheet.m
//  YGActionSheet
//
//  Created by luoyangguang on 16/1/12.
//  Copyright © 2016年 luoyangguang. All rights reserved.
//

#import "YGActionSheet.h"

// 按钮高度
#define BUTTON_H 45.0f
// 屏幕尺寸
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
// 颜色
#define LCColor(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]

@interface YGActionSheet () {
	
	/** 所有按钮 */
	NSArray *_buttonTitles;
	
	/** 暗黑色的view */
	UIView *_darkView;
	
	/** 所有按钮的底部view */
	UIView *_bottomView;
	
	/** 代理 */
	id<YGActionSheetDelegate> _delegate;
}

@property (nonatomic, strong) UIWindow *backWindow;


@end

@implementation YGActionSheet

+ (instancetype)sheetWithTitle:(NSString *)title buttonTitles:(NSArray *)titles redButtonIndex:(NSInteger)buttonIndex delegate:(id<YGActionSheetDelegate>)delegate {
	
	return [[self alloc] initWithTitle:title buttonTitles:titles redButtonIndex:buttonIndex delegate:delegate];
}

- (instancetype)initWithTitle:(NSString *)title
				 buttonTitles:(NSArray *)titles
			   redButtonIndex:(NSInteger)buttonIndex
					 delegate:(id<YGActionSheetDelegate>)delegate {
	
	if (self = [super init]) {
		
		_delegate = delegate;
		
		// 暗黑色的view
		UIView *darkView = [[UIView alloc] init];
		[darkView setAlpha:0];
		[darkView setUserInteractionEnabled:NO];
		[darkView setFrame:(CGRect){0, 0, SCREEN_SIZE}];
		[darkView setBackgroundColor:LCColor(46, 49, 50)];
		[self addSubview:darkView];
		_darkView = darkView;
		
		UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
		[darkView addGestureRecognizer:tap];
		
		// 所有按钮的底部view
		UIView *bottomView = [[UIView alloc] init];
		[bottomView setBackgroundColor:LCColor(233, 233, 238)];
		[self addSubview:bottomView];
		_bottomView = bottomView;
		
		if (title) {
			
			// 标题
			UILabel *label = [[UILabel alloc] init];
			[label setText:title];
			[label setTextColor:LCColor(111, 111, 111)];
			[label setTextAlignment:NSTextAlignmentCenter];
			[label setFont:[UIFont systemFontOfSize:13.0f]];
			[label setBackgroundColor:[UIColor whiteColor]];
			[label setFrame:CGRectMake(0, 0, SCREEN_SIZE.width, BUTTON_H)];
			[bottomView addSubview:label];
		}
		
		if (titles.count) {
			
			_buttonTitles = titles;
			
			NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"YGActionSheet" ofType:@"bundle"];
			
			for (int i = 0; i < titles.count; i++) {
				
				// 所有按钮
				UIButton *btn = [[UIButton alloc] init];
				[btn setTag:i];
				[btn setBackgroundColor:[UIColor whiteColor]];
				[btn setTitle:titles[i] forState:UIControlStateNormal];
				[[btn titleLabel] setFont:[UIFont systemFontOfSize:16.0f]];
				
				UIColor *titleColor = nil;
				if (i == buttonIndex) {
					
					titleColor = LCColor(255, 10, 10);
					
				} else {
					
					titleColor = LCColor(132, 167, 64);
				}
				[btn setTitleColor:titleColor forState:UIControlStateNormal];
				
				NSString *linePath = [bundlePath stringByAppendingPathComponent:@"bgImage_HL@2x.png"];
				UIImage *bgImage = [UIImage imageWithContentsOfFile:linePath];
				
				[btn setBackgroundImage:bgImage forState:UIControlStateHighlighted];
				[btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
				
				CGFloat btnY = BUTTON_H * (i + (title ? 1 : 0));
				[btn setFrame:CGRectMake(0, btnY, SCREEN_SIZE.width, BUTTON_H)];
				[bottomView addSubview:btn];
			}
			
			for (int i = 0; i < titles.count; i++) {
				
				NSString *linePath = [bundlePath stringByAppendingPathComponent:@"cellLine@2x.png"];
				UIImage *lineImage = [UIImage imageWithContentsOfFile:linePath];
				
				// 所有线条
				UIImageView *line = [[UIImageView alloc] init];
				[line setImage:lineImage];
				[line setContentMode:UIViewContentModeCenter];
				CGFloat lineY = (i + (title ? 1 : 0)) * BUTTON_H;
				[line setFrame:CGRectMake(0, lineY, SCREEN_SIZE.width, 1.0f)];
				[bottomView addSubview:line];
			}
		}
		
		// 取消按钮
		UIButton *cancelBtn = [[UIButton alloc] init];
		[cancelBtn setTag:titles.count];
		[cancelBtn setBackgroundColor:[UIColor whiteColor]];
		[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
		[[cancelBtn titleLabel] setFont:[UIFont systemFontOfSize:16.0f]];
		//        [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
		[cancelBtn setTitleColor:LCColor(153, 153, 153) forState:UIControlStateNormal];
		[cancelBtn setBackgroundImage:[UIImage imageNamed:@"bgImage_HL"] forState:UIControlStateHighlighted];
		[cancelBtn addTarget:self action:@selector(didClickCancelBtn) forControlEvents:UIControlEventTouchUpInside];
		
		CGFloat margin = 4.0f;
		CGFloat btnY = BUTTON_H * (titles.count + (title ? 1 : 0)) + margin;
		[cancelBtn setFrame:CGRectMake(0, btnY, SCREEN_SIZE.width, BUTTON_H)];
		[bottomView addSubview:cancelBtn];
		
		CGFloat bottomH = (title ? BUTTON_H : 0) + BUTTON_H * titles.count + BUTTON_H + margin;
		[bottomView setFrame:CGRectMake(0, SCREEN_SIZE.height, SCREEN_SIZE.width, bottomH)];
		
		[self setFrame:(CGRect){0, 0, SCREEN_SIZE}];
		[self.backWindow addSubview:self];
	}
	
	return self;
}

- (UIWindow *)backWindow {
	
	if (_backWindow == nil) {
		
		_backWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
		_backWindow.windowLevel       = UIWindowLevelStatusBar;
		_backWindow.backgroundColor   = [UIColor clearColor];
		_backWindow.hidden = NO;
	}
	
	return _backWindow;
}


- (void)didClickBtn:(UIButton *)btn {
	
	[self dismiss:nil];
	
	if ([_delegate respondsToSelector:@selector(actionSheet:didClickedButtonAtIndex:)]) {
		
		[_delegate actionSheet:self didClickedButtonAtIndex:btn.tag];
	}
}

- (void)dismiss:(UITapGestureRecognizer *)tap {
	
	[UIView animateWithDuration:0.3f
						  delay:0
						options:UIViewAnimationOptionCurveEaseOut
					 animations:^{
						 
						 [_darkView setAlpha:0];
						 [_darkView setUserInteractionEnabled:NO];
						 
						 CGRect frame = _bottomView.frame;
						 frame.origin.y += frame.size.height;
						 [_bottomView setFrame:frame];
						 
					 }
					 completion:^(BOOL finished) {
						 
						 _backWindow.hidden = YES;
						 
						 [self removeFromSuperview];
					 }];
}

- (void)didClickCancelBtn {
	
	[UIView animateWithDuration:0.3f
						  delay:0
						options:UIViewAnimationOptionCurveEaseOut
					 animations:^{
						 
						 [_darkView setAlpha:0];
						 [_darkView setUserInteractionEnabled:NO];
						 
						 CGRect frame = _bottomView.frame;
						 frame.origin.y += frame.size.height;
						 [_bottomView setFrame:frame];
						 
					 }
					 completion:^(BOOL finished) {
						 
						 _backWindow.hidden = YES;
						 
						 [self removeFromSuperview];
						 
						 if ([_delegate respondsToSelector:@selector(actionSheet:didClickedButtonAtIndex:)]) {
							 
							 [_delegate actionSheet:self didClickedButtonAtIndex:_buttonTitles.count];
						 }
					 }];
}

- (void)show {
	
	_backWindow.hidden = NO;
	
	[UIView animateWithDuration:0.3f
						  delay:0
						options:UIViewAnimationOptionCurveEaseOut
					 animations:^{
						 
						 [_darkView setAlpha:0.4f];
						 [_darkView setUserInteractionEnabled:YES];
						 
						 CGRect frame = _bottomView.frame;
						 frame.origin.y -= frame.size.height;
						 [_bottomView setFrame:frame];
						 
					 }
					 completion:nil];
}


@end
